<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 16/5/16
 * Time: 下午9:37
 */

namespace Admin\Controller;

Class ProductAccController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
    }

    public function Product(){
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $keyword = trim(I('get.name',''));
        if($keyword!=''){
            $data = M('product_acc')->where("name like '%$keyword%' and type = 1")->select();
        }else{
            $data = M('product_acc')->where('type = 1')->order(array('order'=>"asc"))->select();
        }
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='产品信息管理';
        $this->url='#';
        $this->assign('product',$data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();
    }

    public function addProduct(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $inf = $upload->upload($_FILES);
            if (!$inf) {// 上传错误提示错误信息
                $data = array(code => 403, msg => $upload->getError());
                $this->ajaxReturn($data, 'JSON');
            }
            $load = I('post.category_type');
            $exp = explode(',',$load);

            $info['path_name'] = $exp[1];
            $info['category_id'] = $exp[0];
            $info['icon'] = $inf[icon][url];
            $info['name'] = trim(I('post.product_name'));
            $info['url'] = trim(I('post.url'));
            $info['price'] = trim(I('post.price'));
            $info['order'] = trim(I('post.order'));
            $info['status'] = I('post.status');
            $info['type'] = '1'; //1产品2配件
            $info['add_time'] = time();
            $end = M('product_acc')->add($info);
            if($end){
                operatelog('产品信息管理','1',$end);
                echo 1;
                exit;
            } else {
                echo 2;
                exit;
            }
        }
        $category = M('category')->field('category_id,path_name')->where('pid != 0')->select();
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='产品信息管理';
        $this->breadcrumb3='新增产品';
        $this->url=U('ProductAcc/Product');
        $this->assign('select',$category);
        $this->display();
    }

    public function editProduct(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $inf = $upload->upload($_FILES);
            if ($inf) {// 上传错误提示错误信息
                $info['icon'] = $inf[icon][url];
            }
            $load = I('post.category_type');
            $exp = explode(',',$load);
            $id = I('post.id');
            $info['name'] = trim(I('post.product_name'));
            $info['url'] = trim(I('post.url'));
            $info['price'] = trim(I('post.price'));
            $info['category_id'] = $exp[0];
            $info['path_name'] = $exp[1];
            $info['order'] = trim(I('post.order'));
            $info['status'] = I('post.status');
            $data = M('product_acc')->where("id = $id")->save($info);

            if($data){
                operatelog('产品信息管理','3',$id);
                echo 3;
                exit;
            }else{
                echo 2;
                exit;
            }

        }
        $id = I('get.id');
        $product = M('product_acc')->where("id = $id")->select();
        $category = M('category')->field('category_id,path_name')->where('pid != 0')->select();
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='产品信息管理';
        $this->breadcrumb3='编辑';
        $this->url=U('ProductAcc/Product');
        $this->assign('select',$category);
        $this->assign('product',$product);
        $this->display();
    }

    public function editAcc(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $inf = $upload->upload($_FILES);
            if ($inf) {// 上传错误提示错误信息
                $info['icon'] = $inf[icon][url];
            }
            $load = I('post.category_type');
            $exp = explode(',',$load);
            $id = I('post.id');
            $info['name'] = trim(I('post.acc_name'));
            $info['url'] = trim(I('post.url'));
            $info['price'] = trim(I('post.price'));
            $info['category_id'] = $exp[0];
            $info['path_name'] = $exp[1];
            $info['order'] = trim(I('post.order'));
            $info['status'] = I('post.status');
            $data = M('product_acc')->where("id = $id")->save($info);
            if($data){
                operatelog('配件信息管理','3',$id);
                echo 3;
                exit;
            }else{
                echo 2;
                exit;
            }

        }
        $id = I('get.id');
        $product = M('product_acc')->where("id = $id")->select();
        $category = M('category')->field('category_id,path_name')->where('pid != 0')->select();
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='配件信息管理';
        $this->breadcrumb3='编辑';
        $this->url=U('ProductAcc/Accessories');
        $this->assign('select',$category);
        $this->assign('product',$product);
        $this->display();
    }

    public function Accessories(){
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $keyword = trim(I('get.name',''));
        if($keyword!=''){
            $data = M('product_acc')->where("name like '%$keyword%' and type = 2")->select();
        }else{
            $data = M('product_acc')->where('type = 2')->order(array('order'=>"asc"))->select();
        }
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='配件信息管理';
        $this->url='#';
        $this->assign('product',$data);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();
    }


    public function addAcc(){
        if(IS_POST){
            $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
            $inf = $upload->upload($_FILES);
            if (!$inf) {// 上传错误提示错误信息
                $data = array(code => 403, msg => $upload->getError());
                $this->ajaxReturn($data, 'JSON');
            }
            $load = I('post.category_type');
            $exp = explode(',',$load);

            $info['path_name'] = $exp[1];
            $info['category_id'] = $exp[0];
            $info['icon'] = $inf[icon][url];
            $info['name'] = trim(I('post.acc_name'));
            $info['url'] = trim(I('post.url'));
            $info['price'] = trim(I('post.price'));
            $info['order'] = trim(I('post.order'));
            $info['status'] = I('post.status');
            $info['type'] = '2'; //1产品2配件
            $info['add_time'] = time();
            $end = M('product_acc')->add($info);
            if($end){
                operatelog('配件信息管理','1',$end);
                echo 1;
                exit;
            } else {
                echo 2;
                exit;
            }
        }
        $category = M('category')->field('category_id,path_name')->where('pid != 0')->select();
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='配件信息管理';
        $this->breadcrumb3='新增配件';
        $this->url=U('ProductAcc/Accessories');
        $this->assign('select',$category);
        $this->display();
    }

    public function Del(){
        $product = I('post.product','');
        $acc = I('post.acc','');
        if($product==''){
            $cid = $acc;
        }else{
            $cid = $product;
        }
        $id = implode(',',$cid);
        $maps['id'] = array('in',$id);
        $data = M('product_acc')->where($maps)->delete();
        if($data){
            if($product=='') {
                operatelog('配件信息管理','2',$id);
                echo 1;
                exit;
            }else{
                operatelog('产品信息管理','2',$id);
                echo 2;
                exit;
            }
        }else{
            echo -1;
            exit;
        }
    }


}